<template>
  <el-container class="home-container">
    <!-- 头部区域           -->
    <el-header>
      <div>
        <img src="../assets/jingdong.jpeg" alt class="picture" />
        <span>京东电商后台管理系统</span>
      </div>
      <el-button type="danger" @click="logout">退出登录</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#373d41"
          text-color="#fff"
          active-text-color="#e83632"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <i class="iconfont" :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <!-- 路由改造！！！！ -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavstatus('/'+subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-help iconfont"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menulist: [],
      // 字体图片的循环遍历方法！！！
      iconsObj: {
        125: "el-icon-user-solid",
        103: "el-icon-s-release",
        101: "el-icon-s-goods",
        102: "el-icon-s-home",
        145: "el-icon-s-data",
      },
      isCollapse: false,
      activePath: "",
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem("activePath")
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push("/login")
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("menus")
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      } else {
        this.menulist = res.data
        console.log(this.menulist)
      }
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavstatus(activePath) {
      window.sessionStorage.setItem("activePath", activePath)
      this.activePath = activePath
    },
  },
}
</script>
<style lang="less" scoped>
.home-container {
  width: 100%;
  height: 100%;
}
.el-button {
  background-color: #e83632;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #eaedf1;
  font-size: 20px;
  div {
    display: flex;
    align-items: center;
    .picture {
      width: 50px;
      border-radius: 10px;
      margin-right: 20px;
    }
  }
}
.el-aside {
  background-color: #373d41;
  .el-menu {
    border-right: null;
    width: 100%;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 15px;
}
.toggle-button {
  background-color: #333744;
  font-size: 10px;
  line-height: 24px;
  color: aliceblue;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>